package com.neardrug.services.impl;

import com.neardrug.common.Constants;
import com.neardrug.controller.dto.FormHTMLUpdateDTO;
import com.neardrug.entity.FormImg;
import com.neardrug.entity.FormTable;
import com.neardrug.exception.ServiceException;
import com.neardrug.mapper.FormImgMapper;
import com.neardrug.mapper.FormTableMapper;
import com.neardrug.services.FormService;
import com.neardrug.vo.FormVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FormServiceImpl implements FormService {
    @Resource
    private FormImgMapper formImgMapper;
    @Resource
    private FormTableMapper formTableMapper;
    /**
     * 根据任务id查询出对应的“表格页面”的数据
     * @param taskId
     * @return
     */
    @Override
    public FormVO selectByTaskId(Integer taskId) {
        List<FormImg> formImgList=formImgMapper.getByTaskId(taskId);
        List<FormTable> formTableList=formTableMapper.getByTaskId(taskId);
        FormVO formVO=FormVO.builder()
                .taskId(taskId)
                .imgList(formImgList)
                .htmlList(formTableList)
                .build();
        return formVO;
    }

    /**
     * 修改某个id的html表格
     * @param formHTMLUpdateDTO
     */
    @Override
    public void updateHTML(FormHTMLUpdateDTO formHTMLUpdateDTO) {
        Integer id = formHTMLUpdateDTO.getId();
        if (id == null){
            throw new ServiceException(Constants.PARAM_ERROR,"参数为空");
        }
        String html = formHTMLUpdateDTO.getHtml();
        formTableMapper.updateHTML(id,html);
    }

    @Override
    public void deleteHtmlById(Integer id) {
        Integer flag=formTableMapper.deleteHtmlById(id);
        if (flag==0){
            throw new ServiceException(Constants.CODE_500,"删除失败");
        }
    }
}
